package user

import (
	"github.com/bgmp/config"
	"github.com/bgmp/handler"
	"github.com/bgmp/middleware"
	"github.com/bgmp/model"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
	"net/http"
)

func (b Base) AddBg(c *gin.Context, req *AddBgRequest) *AddBgResponse {
	if req.Type == model.BgNull || req.Value == 0 {
		handler.ReplyError(c, http.StatusBadRequest, "请正确传入参数！")
		return nil
	}

	bg := model.Bg{
		// 元数据
		BgMeta: model.BgMeta{
			// 从中间件中取到 id
			UserId: middleware.GetUserId(c),
		},
		BgField: model.BgField{
			Time:  req.Time.Time,
			Value: req.Value,
			Type:  req.Type,
		},
	}
	err := bg.Create(config.DB)
	if err != nil {
		handler.Errorf(c, "添加记录失败")
		log.Error(err)
		return nil
	}
	//go service.TrySavePredict(middleware.GetUserId(c), req.Time)
	return &AddBgResponse{
		// 自动会返回添加数据的ID
		Id: bg.ID,
	}
}

// DTO
type AddBgRequest struct {
	Time  model.Time   `json:"time"`
	Value float64      `json:"value"`
	Type  model.BgType `json:"type"`
}

// VO
type AddBgResponse struct {
	Id uint `json:"id"`
}
